package com.xywy.beautyand.ACT;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.xywy.beautyand.R;
import com.xywy.beautyand.base.BaseActivity;
import com.xywy.beautyand.bean.User;
import com.xywy.beautyand.util.Constants;
import com.xywy.beautyand.util.MD5;
import com.xywy.beautyand.util.UIUtil;
import com.xywy.beautyand.vollery.OnVolleyResponseListener;
import com.xywy.beautyand.vollery.PostRequest;

public class PersonDataUpdate extends BaseActivity {
	private int intent_for_update; // 从哪个界面 用过的
	public static final int NAME_UPDATE = 0;
	public static final int SEX_UPDATE = 1;
	public static final int HEIGHT_UPDATE = 2;
	public static final int WEIGHT_UPDATE = 3;

	private EditText et_update_info;
	private LinearLayout lin_sex;
	private RadioButton male, female;
	private RelativeLayout left_img;
	private User temp_user;
	RelativeLayout rl_me_infor_update_height;
	private LinearLayout rl_et_content;
	private EditText et_height;
	private TextView tv_me_height_cancel_font;
	private SharedPreferences sp;

	private TextView middle_title;
	private TextView right_title;
	private LinearLayout tobar;
	private ImageView right_img;

	@Override
	protected void initView(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.act_person_update);
		// initToBar();
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		middle_title = (TextView) findViewById(R.id.middle_title);
		right_title = (TextView) findViewById(R.id.right_text);
		right_title.setVisibility(View.VISIBLE);
		right_img = (ImageView) findViewById(R.id.right_img);

		et_update_info = (EditText) findViewById(R.id.et_update);
		lin_sex = (LinearLayout) findViewById(R.id.lin_sex);
		male = (RadioButton) findViewById(R.id.male);
		female = (RadioButton) findViewById(R.id.female);
		lin_sex.setVisibility(View.GONE);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		right_title.setText("保存");
		right_title.setVisibility(View.VISIBLE);
		rl_me_infor_update_height = (RelativeLayout) findViewById(R.id.rl_me_infor_update_height);
		rl_me_infor_update_height.setVisibility(View.GONE);
		rl_et_content = (LinearLayout) findViewById(R.id.rl_et_content);
		et_height = (EditText) findViewById(R.id.et_me_infor_update_height);
		right_title.setOnClickListener(this);
		tv_me_height_cancel_font = (TextView) findViewById(R.id.iv_me_height_cancel_font);
		initIntent();
	}

	private void initIntent() {
		user = User.getInstance(context);
		temp_user = User.getInstance(context);
		Intent intent = getIntent();
		sp = getSharedPreferences("config", MODE_PRIVATE);
		int weight = sp.getInt("weight", 0);
		intent_for_update = intent.getIntExtra("intent_for_update", -1);
		switch (intent_for_update) {
		case NAME_UPDATE:
			middle_title.setText("用户名");
			et_update_info.setHint("请输入用户名");
			et_update_info.setText(user.getAccountstr());
			break;
		case SEX_UPDATE:
			lin_sex.setVisibility(View.VISIBLE);
			et_update_info.setVisibility(View.GONE);
			middle_title.setText("性别");
			if ("女".equals(user.getSex())) {
				female.setChecked(true);
			} else {
				male.setChecked(true);
			}
			et_update_info.setText(user.getSex());
			break;
		case HEIGHT_UPDATE:
			rl_et_content.setVisibility(View.GONE);
			rl_me_infor_update_height.setVisibility(View.VISIBLE);
			middle_title.setText("身高");
			et_update_info.setHint("请输入身高");
			et_height.setInputType(EditorInfo.TYPE_CLASS_PHONE);
			et_height.setInputType(EditorInfo.TYPE_CLASS_PHONE);
			et_height.setText(user.getHeight());
			break;
		case WEIGHT_UPDATE:
			rl_et_content.setVisibility(View.GONE);
			rl_me_infor_update_height.setVisibility(View.VISIBLE);
			tv_me_height_cancel_font.setText("kg");
			middle_title.setText("体重");
			et_height.setHint("请输入体重");
			et_height.setInputType(EditorInfo.TYPE_CLASS_PHONE);
			et_height.setInputType(EditorInfo.TYPE_CLASS_PHONE);
			if (weight == 0) {
			} else {
				et_height.setText(weight + "");
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void initListener() {
		// TODO Auto-generated method stub
		left_img.setOnClickListener(this);
	}

	@Override
	protected void initData() {
		temp_user.setAccountstr(user.getAccountstr());
		temp_user.setBirthday(user.getBirthday());
		temp_user.setCode(user.getCode());
		temp_user.setHeight(user.getHeight());
		temp_user.setId(user.getId());
		temp_user.setIsregister(user.getIsregister());
		temp_user.setPhonenum(user.getPhonenum());
		temp_user.setSex(user.getSex());
		temp_user.setState(user.getState());
		temp_user.setUserid(user.getUserid());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.right_text:
			changeUserInfo();
			break;
		case R.id.left_img:
			finish();
			break;
		default:
			break;
		}
	}

	private User user;
	public static String updateUrl = Constants.url
			+ "act=kbb&fun=resetProperty";

	private void changeUserInfo() {
		String et_content = et_update_info.getText().toString().trim();
		switch (intent_for_update) {
		case NAME_UPDATE:
			if (et_content == null || "".equals(et_content)) {
				Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
				return;
			}
			temp_user.setAccountstr(et_content);
			break;
		case HEIGHT_UPDATE:
			String height = et_height.getText().toString().trim();
			if (height == null || "".equals(height)) {
				Toast.makeText(this, "身高不能为空", Toast.LENGTH_SHORT).show();
				return;
			}
			temp_user.setHeight(height);
			break;
		case SEX_UPDATE:
			if (male.isChecked()) {
				temp_user.setSex("男");
			} else if (female.isChecked()) {
				temp_user.setSex("女");
			}
			break;
		case WEIGHT_UPDATE:
			String weight = et_height.getText().toString().trim();
			if (weight == null || "".equals(weight)) {
				Toast.makeText(this, "体重不能为空", Toast.LENGTH_SHORT).show();
				return;
			}
			int w = Integer.parseInt(weight);
			sp.edit().putInt("weight", w).commit();
			Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
			break;

		default:
			break;
		}

		HashMap<String, String> params = new HashMap<String, String>();
		params.put("userid", temp_user.getUserid());
		params.put("accountstr", temp_user.getAccountstr());
		params.put("sex", temp_user.getSex());
		params.put("height", temp_user.getHeight());
		params.put("weight", 65 + "");
		params.put("birthday", temp_user.getBirthday());
		params.put("keyword", "edit");
		params.put("tag", Constants.wjkTag);
		String sign;
		try {
			sign = MD5.md5s(Constants.Sign + Constants.wjkTag);
			params.put("sign", sign);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		PostRequest<String> request = new PostRequest<String>(updateUrl,
				String.class, new OnVolleyResponseListener<String>() {

					@Override
					public void onError(VolleyError volleyError) {
						// TODO Auto-generated method stub
						System.out.println(volleyError.getCause());
						updateHandler.sendEmptyMessage(1);
					}

					@Override
					public void onSuccess(String response) {
						// TODO Auto-generated method stub
						try {
							System.out.println(response);
							JSONObject json = new JSONObject(response);
							int sate = json.optInt("state");
							if (sate == 200) {
								updateHandler.sendEmptyMessage(0);
								temp_user.setAccountstr(user.getAccountstr());
								temp_user.setBirthday(user.getBirthday());
								temp_user.setCode(user.getCode());
								temp_user.setHeight(user.getHeight());
								temp_user.setId(user.getId());
								temp_user.setIsregister(user.getIsregister());
								temp_user.setPhonenum(user.getPhonenum());
								temp_user.setSex(user.getSex());
								temp_user.setState(user.getState());
								temp_user.setUserid(user.getUserid());
							} else {
								updateHandler.sendEmptyMessage(1);
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
		request.setIsParseJson(false);
		request.setParams(params);
		executeRequest(request);
	}

	private Handler updateHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 0:
				UIUtil.DissDialog(context, waittingDialog);
				Toast.makeText(PersonDataUpdate.this, "更改成功",
						Toast.LENGTH_SHORT).show();
				break;
			case 1:
				UIUtil.DissDialog(context, waittingDialog);
				Toast.makeText(PersonDataUpdate.this, "更改失败,请重试",
						Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		};
	};

}
